package cn.j2ee.chapter2.db.transaction;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.InvalidTransactionException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

/**
 * 
 * 2016年11月21日 下午6:08:17
 */
public class TransactionManagerImpl implements TransactionManager {
	// 此处 transactionHolder 用于将 Transaction 所代表的事务对象关联到线程上
	private static ThreadLocal<TransactionImpl> transactionHolder = new ThreadLocal<TransactionImpl>(); 
		
	 //TransacationMananger 必须维护一个全局对象，因此使用单实例模式实现
	 private static TransactionManagerImpl singleton = new TransactionManagerImpl(); 
	
	 private TransactionManagerImpl(){ 
		
	 } 
	
	 public static TransactionManagerImpl singleton(){ 
		 return singleton; 
	 } 
	@Override
	public void begin() throws NotSupportedException, SystemException {
		//XidImpl 实现了 Xid 接口，其作用是唯一标识一个事务
		 XidImpl xid = new XidImpl(); 
		// 创建事务对象，并将对象关联到线程
//		 TransactionImpl tx = new TransactionImpl(xid); 
		
//		 transactionHolder.set(tx); 
	}

	@Override
	public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
			SecurityException, IllegalStateException, SystemException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getStatus() throws SystemException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Transaction getTransaction() throws SystemException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void resume(Transaction arg0) throws InvalidTransactionException, IllegalStateException, SystemException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void rollback() throws IllegalStateException, SecurityException, SystemException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setRollbackOnly() throws IllegalStateException, SystemException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setTransactionTimeout(int arg0) throws SystemException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Transaction suspend() throws SystemException {
		// TODO Auto-generated method stub
		return null;
	}

}
